package com.peng.leetcode.array;

import java.util.Arrays;

/**
 * RemoveDuplicates
 * <p>
 * 80. 删除排序数组中的重复项 II
 *
 * @author: lupeng6
 * @create: 2021/1/13 20:25
 */
public class RemoveDuplicates {

    public static void main(String[] args) {
        int[] arr = new int[]{1, 1, 1, 2, 2, 3};
        int i = new RemoveDuplicates().removeDuplicates(arr);
        System.out.println(i);
        Arrays.stream(arr).forEach(System.out::println);
    }

    public int removeDuplicates(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int length = nums.length;;
        int count = 0;
        int index = 1;

        while (index < length) {
            if (nums[index - 1] == nums[index]) {
                count++;
                if (count >= 2) {
                    remove(nums, index);
                    index--;
                    length--;
                }
            } else {
                count = 0;
            }
            index++;
        }
        return length;
    }

    private static int[] remove(int[] arr, int index) {
        for (int i = index + 1; i < arr.length; i++) {
            arr[i - 1] = arr[i];
        }
        return arr;
    }
}
